using System;

namespace SharpMud.MudLib.SAMPLE.Net.SessionStates.PlayerCreation
{
	public class StartCreation : SessionStateBase
	{
		public string _PlayerName;

		private StartCreation()
			:base()
		{
		}
		public StartCreation(string playerName)
			:this()
		{
			_PlayerName = playerName;
		}

		public override string StateID
		{
			get
			{
				return "Creation";
			}
		}

		public override void Pulse()
		{
			if(base.StillActive)
			{
				Session.CharacterInCreation = new SharpMud.MudLib.SAMPLE.Entities.PlayerCharacter();
				Session.CharacterInCreation.PlayerName = this._PlayerName;

				Session.WriteLine();
				GetPassword();
				Session.WriteLine();

				this.SetInactive();
				Session.SessionState.Clear();
				Session.SessionState.Push(new ConcludeCreation());
			}
		}

		public void GetPassword()
		{
			string sPassword1 = "";
			string sPassword2 = "";
				
			//Start out password acquisition with explanation
			Session.WriteLine("You must choose a password.");
			Session.WriteLine("Choose well, it will control access to your player character.");

			while(true)
			{
				//Get password first time
				Session.Agents.RemoteEcho.TurnFeatureOff();
				sPassword1 = Session.Prompt("What will be your password? ");
				Session.Agents.RemoteEcho.TurnFeatureOn();
				while(!MudServer.PlayerSystem.IsValidPassword(sPassword1))
				{
					Session.WriteLine("That is not a valid password. Try again.");
					Session.Agents.RemoteEcho.TurnFeatureOff();
					sPassword1 = Session.Prompt("What will be your password? ");
					Session.Agents.RemoteEcho.TurnFeatureOn();
					Session.WriteLine();
				}
				//Get password, second time
				Session.WriteLine("You must type your password again to verify it.");
				Session.Agents.RemoteEcho.TurnFeatureOff();
				sPassword2 = Session.Prompt("What was your password? ");
				Session.Agents.RemoteEcho.TurnFeatureOn();
				Session.WriteLine();
				
				if(sPassword1!=sPassword2)
				{
					Session.WriteLine("The passwords you gave do not match each other.");
				}
				if(sPassword1.ToLower()==sPassword2.ToLower())
				{
					Session.WriteLine("Reminder: Passwords are case sensitive.");
				}
				if(sPassword1==sPassword2)
					break;
				Session.WriteLine("Please try again.");
			}
			Session.CharacterInCreation.SetPassword(sPassword1);
		}
	}
}
